package com.sikaryofficial.backend.manager;

import cn.hutool.core.collection.CollUtil;
import com.sikaryofficial.backend.annotation.TenantFilter;
import com.sikaryofficial.backend.domain.dto.req.StoreFollowReq;
import com.sikaryofficial.backend.domain.dto.req.StoreFollowSaveListReq;
import com.sikaryofficial.backend.domain.dto.resp.StoreFollowDTO;
import com.sikaryofficial.backend.domain.entity.StoreFollow;
import com.sikaryofficial.backend.domain.mapping.StoreFollowMapping;
import com.sikaryofficial.backend.service.IAttachmentService;
import com.sikaryofficial.backend.service.IStoreFollowService;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import com.sikaryofficial.system.api.model.att.AttachmentResp;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : wuweihong
 * @desc : 店铺回访记录服务
 * @date : 2025-01-03
 */

@Service
public class StoreFollowManager {

	@Autowired
	private IStoreFollowService storeFollowService;

	@Autowired
	private IAttachmentService attachmentService;

	/**
	 * 批量保存回访记录
	 *
	 * @param
	 */
	@TenantFilter
	public void saveBatch(StoreFollowSaveListReq saveListReq) {
		List<StoreFollowReq> reqs = saveListReq.getStoreFollows();
		reqs.forEach(item -> item.setStoreId((saveListReq.getStoreId())));
		reqs.forEach(this::save);
	}

	/**
	 * 保存单条回访记录
	 *
	 * @param req
	 */
	@TenantFilter
	private void save(StoreFollowReq req) {
		StoreFollow storeFollow = StoreFollowMapping.INSTANCE.coverReqToEntity(req);
		storeFollowService.save(storeFollow);
		attachmentService.saveAttachmentList(storeFollow.getStoreFollowId(), req.getImgList(), SecurityUtils.getUserId());
	}

	/**
	 * 获取回访记录列表
	 *
	 * @param storeId
	 * @return
	 */
	@TenantFilter
	public List<StoreFollowDTO> getFollowupList(Long storeId) {
		List<StoreFollow> storeFollows = storeFollowService.lambdaQuery().eq(StoreFollow::getStoreId, storeId).eq(StoreFollow::getDeletedVersion, 0).list();
		if (CollUtil.isEmpty(storeFollows)) {
			return Collections.emptyList();
		}
		List<StoreFollowDTO> storeFollowDTOList = StoreFollowMapping.INSTANCE.coverListToResp(storeFollows);
		List<Long> storeFollowIds = storeFollows.stream().map(StoreFollow::getStoreFollowId).collect(Collectors.toList());
		Map<Long, List<AttachmentResp>> attachmentMap = attachmentService.getAttachmentByIds(storeFollowIds);
		storeFollowDTOList.forEach(item -> item.setImgList(MapUtils.getObject(attachmentMap, item.getStoreFollowId())));
		return storeFollowDTOList;
	}

	/**
	 * 删除单条回访记录
	 *
	 * @param storeFollowId
	 * @return
	 */
	@TenantFilter
	public boolean deleteByFollowId(Long storeFollowId) {
		
		return storeFollowService.lambdaUpdate()
				.eq(StoreFollow::getStoreFollowId, storeFollowId)
				.eq(StoreFollow::getDeletedVersion, 0)
				.setSql(" deleted_version=store_follow_id ")
				.update();
	}

}
